package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dao.EmployeeDao;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * reggie-com.itheima.service.impl
 * Administrator
 * 2023-03-06
 * 16:06
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements
EmployeeService{

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public Map checkLogin(Employee emp) {
        HashMap<Object, Object> map = new HashMap<>();
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,emp.getUsername());
        Employee employee = employeeDao.selectOne(lqw);
        //List<Employee> list = employeeService.list(lqw);
        if (employee==null){
            map.put("msg","用户名错误");
            return map;
        }
        String s = MD5Util.string2MD5(emp.getPassword());
        System.out.println("加密后密码"+s);
        lqw.eq(Employee::getPassword,s);
        Employee employee1 = employeeDao.selectOne(lqw);
        if (employee1==null){
            map.put("msg","密码错误");
            return map;
        }
        if (employee1.getStatus()==0){
            map.put("msg","账户被封");
            return map;
        }
        //登陆成功
        map.put("emp",employee);
        return map;
    }

    @Override
    public boolean add(Employee emp) {
        int insert = employeeDao.insert(emp);
        return insert!=0;
    }
}
